package com.etime.shycourse.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.etime.shycourse.dao.StudyRecordDao;
import com.etime.shycourse.pojo.StudyRecord;
import com.etime.shycourse.services.interfaces.StudyRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 学习记录管理service层实现类
 */
@Service
public class StudyRecordServiceImpl implements StudyRecordService {

    @Autowired
    private StudyRecordDao studyRecordDao;

    /**
     * 插入学习记录
     *
     * @param record
     * @return
     */
    @Override
    public synchronized int saveStudyRecord(StudyRecord record) {
        int rtn = 1;
        //1、判断用户是否有记录（根据用户id，课程id，课时id）
        StudyRecord studyRecord = selectStudyRecord(record);
        //2、如果有记录，并且状态为“已完成=2），则不进行任何操作
        if(studyRecord!=null) {
            //3、如果有记录，并且状态为“未完成!=2”，则进行修改操作；
            if(studyRecord.getRlessonstate()!=2){
                //判断当前进度比原来记录。如果当前进度低于原来的进度，则不保存
                if(record.getRlessonrecord()>studyRecord.getRlessonrecord()){
                    studyRecord.setRlessonrecord(record.getRlessonrecord());
                    studyRecord.setEntime(new Date());
                    studyRecord.setRlessonstate(record.getRlessonstate());
                    rtn = studyRecordDao.updateById(studyRecord);
                }
            }
        }else{
            //4、如果没有记录，则进行插入操作
            record.setEntime(new Date());
            rtn = studyRecordDao.insert(record);
        }
        return rtn;
    }

    /**
     * 根据课时id和用户id，查询该用户在此课程的学习进度
     *
     * @param userid   用户id
     * @param lessonid 课程id
     * @return
     */
    @Override
    public StudyRecord selectStudyRecordByLessonId(Integer userid, Integer lessonid) {
        QueryWrapper<StudyRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("ruserid",userid);
        wrapper.eq("rlessonid",lessonid);
        List<StudyRecord> list = studyRecordDao.selectList(wrapper);
        if(list.size()>0){
            return list.get(0);
        }else {
            return null;
        }
    }

    /**
     * 根据课程id和用户id，查询该用户对于此课程的最后学习记录
     *
     * @param userid
     * @param courseid
     * @return
     */
    @Override
    public StudyRecord selectStudyRecordByCourseId(Integer userid, Integer courseid) {
        StudyRecord record = new StudyRecord();
        record.setRuserid(userid);
        record.setRcourseid(courseid);
        return studyRecordDao.selectStudyRecordByCourseId(record);
    }

    /**
     * 查询学习记录
     * @param record
     * @return
     */
    private StudyRecord selectStudyRecord(StudyRecord record){
        QueryWrapper<StudyRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("ruserid",record.getRuserid());
        wrapper.eq("rcourseid",record.getRcourseid());
        wrapper.eq("rlessonid",record.getRlessonid());
        List<StudyRecord> list = studyRecordDao.selectList(wrapper);
        if(list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }
}
